package com.javatest.springboot2basictest.response;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-07-03 上午9:28
 */
public class ResponseMessage {
    private Integer code;
    private String msg;
    private Object data;

    public ResponseMessage() {
        code = 200;
        msg = "success";
    }

    public ResponseMessage(Object data) {
        code = 200;
        msg = "success";
        this.data = data;
    }

    public ResponseMessage(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseMessage(ResultCode code, String msg, Object data) {
        this.code = code.code;
        this.msg = msg;
        this.data = data;
    }

    public static Builder builder() {
        return new Builder();
    }

    @Override
    public String toString() {
        return "ResponseMessage{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * builder模式，支持链式赋值
     */
    public static final class Builder {
        private Integer code;
        private String msg;
        private Object data;

        private Builder() {
        }

        public Builder setCode(Integer code) {
            this.code = code;
            return this;
        }

        public Builder setCode(ResultCode code) {
            this.code = code.code;
            return this;
        }

        public Builder setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder setData(Object data) {
            this.data = data;
            return this;
        }

        public ResponseMessage build() {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setCode(code);
            responseMessage.setMsg(msg);
            responseMessage.setData(data);
            return responseMessage;
        }
    }
}